define(['app','pagination','datetime','editor'], function( appCRM ) {
'use strict';
/*呼叫控制(个人部分)*/
appCRM.controller('Call', function($scope, dataFactoryCRM, $q) {
  $scope.calllog;
  $scope.calllogpage = {pageAll: 1,pageNow: 1, pageSize:$scope.psize,k: 3};      /*通话记录分页*/
  $scope.calllogpage2 = {pageAll: 1,pageNow: 1, pageSize:20,k: 3};      /*时长统计分页*/
  $scope.callTypes =         /*搜索通话类型，对应数据库中key值减1*/
    [
    {k: '1',v: '打入'}, 
    {k: '2',v: '打出'},
    {k: '3',v: '呼叫转移入'},
    {k: '4',v: '呼叫转移出'},
    {k: '5',v: '内部通话'},
    {k: '6',v: '双向外呼'}
    ]; 
    $scope.callTypes2 =         /*搜索通话类型，对应数据库中key值减1，内部通话不统计时长*/
    [
    {k: '1',v: '打入'}, 
    {k: '2',v: '打出'},
    {k: '3',v: '呼叫转移入'},
    {k: '4',v: '呼叫转移出'},
    {k: '6',v: '双向外呼'}
    ]; 
  //获取今天日期
  var d = new Date();
  var years = d.getFullYear();
  var months = d.getMonth()+1;          //d.getMonth()范围0-11
  if(months<10) months = "0" +""+ months;   //月份不足两位补0
  var days = d.getDate();
  if(days<10) days = "0"+""+days;       //日期不足两位补0
    /*时长统计模块*/
  $scope.key;                    /*查看方式：今日、本周、本月、全部*/
  $scope.search_begintime = '';  /*搜索：开始时间*/
  $scope.search_endtime = '';   /*搜索：结束时间*/
  $scope.search_type;      /*搜索：通话类型*/
  $scope.cal_times;         /*通话次数*/
  $scope.dial_cal_time;     /*拨打电话时长*/
  $scope.anser_cal_time;     /*累计接听电话*/
  $scope.cal_time;           /*通话时长*/
  $scope.per_cal_time;      /*每通话电话时长*/
  $scope.get_today;       /*默认显示今天通话时长*/
  $scope.call_log_list;    /*获取本周、本月或者某时间段通话列表*/
  $scope.eff_cal_times;    /*有效通话次数*/
  $scope.date_shows;        /*本周起止时间数组*/
  /*系统错误调用*/
    $scope.msgAttention = function() {
        $('.ui.systemerror').modal({
            closable: false,
            onDeny: function() {
                window.alert('Wait not yet!');
                return false;
            },
            onApprove: function() {
                $('.ui.dimmer.modals.page.transition.visible.active.dimmers').removeClass('dimmers');
            }
        }).modal('show');

        setTimeout(function() {
            var system = $('.ui.dimmer.modals.page.transition.visible.active');
            if (system) {
                $('.ui.dimmer.modals.page.transition.visible.active').addClass('dimmers');
            }
        }, 1000);
    };
  //获取后台时长统计timeStatistics：今日、全部
  $scope.getTimeStatistics = function(){
    $scope.search_begintime = document.getElementById('begintime').value;
    $scope.search_endtime = document.getElementById('endtime').value;
    $scope.search_type = document.getElementById('c_type').value;
    var _url = 'call/timeStatistics?key='+$scope.key+'&begintime='+$scope.search_begintime+'&endtime='+$scope.search_endtime+'&type='+$scope.search_type;
        $scope.$emit("BUSY");
        dataFactoryCRM.getDataByAPI(_url).then(function(d) {
      if (d.rcode == "1") {
          $scope.cal_times = d.cal_times;
          $scope.eff_cal_times = d.eff_cal_times;
          $scope.callinfo_show = d.callinfo_show;   //总时长统计列表
          $scope.eff_cal_times = d.eff_cal_times;
          $scope.dial_cal_time = d.dial_cal_time;
          $scope.anser_cal_time = d.anser_cal_time;
          $scope.cal_time = d.cal_time;
          $scope.date_shows = d.date_shows;
          $scope.per_cal_time = d.per_cal_time;
          $("#call_log_lists").show();
          $("#show_calllog_info").show();
          $("#no_calllog_info").hide();         
      } else if (d.rcode == "2") {
          $("#call_log_lists").show();
          $("#show_calllog_info").hide();
          $("#no_calllog_info").show();
      }
      $("#change_trend").hide();    //隐藏下方时间段内时长统计列表
      $scope.$emit("NOTBUSY");
    });
  }

  $scope.week_shows;   /*显示本周每天日期*/
  $scope.month_shows;   /*显示本月梅泰诺日期*/
  $scope.eff_cal_times;   /*有效通话次数*/
  //获取后台时长统计(+时间段)通话列表：本周、本月、某时间段
  $scope.getChangeTrends = function(){
    $scope.search_begintime = document.getElementById('begintime').value;
    $scope.search_endtime = document.getElementById('endtime').value;
    $scope.search_type = document.getElementById('c_type').value;

    var _url = 'call/getChangeTrends?key='+$scope.key+'&begintime='+$scope.search_begintime+'&endtime='+$scope.search_endtime+'&type='+$scope.search_type;
    if($scope.search_begintime && $scope.search_endtime && $scope.search_begintime < $scope.search_endtime){
            //某时间段内时长统计时，查看方式按钮全部去掉选中状态
      _url += '&p=' + $scope.calllogpage2.pageNow + '&psize=' + $scope.calllogpage2.pageSize;
      $("#demo4").removeClass("selected");$("#demo1").removeClass("selected");$("#demo2").removeClass("selected");$("#demo3").removeClass("selected");
    }
      $scope.$emit("BUSY");
      dataFactoryCRM.getDataByAPI(_url).then(function(d) {
      $scope.rcode = d.rcode;
       if (d.cal_times ) {
          $scope.call_log_list = d.call_list;
          $scope.time_shows = d.time_show_2;
          $scope.day = d.day;
          
          $scope.week_shows = d.week_shows;
          $scope.month_shows = d.month_show_2;
          
          $scope.cal_times = d.cal_times;
          $scope.eff_cal_times = d.eff_cal_times;
          $scope.callinfo_show = d.callinfo_show;   //总时长统计列表
          $scope.cal_time = d.cal_time;
          if($scope.search_begintime && $scope.search_endtime && $scope.search_begintime < $scope.search_endtime){
              $scope.calllogpage2.pageSize = d.psize;
              $scope.calllogpage2.pageAll = d.pall;
              $scope.calllogpage2.pageNow = d.p;
              $scope.time_shows = d.time_show_2;
          }else if($scope.search_begintime && $scope.search_endtime && $scope.search_begintime == $scope.search_endtime){
            $("#change_trend").hide();
            $("#wenzi").hide();
          }
          
          $("#call_log_lists").show();
          $("#show_calllog_info").show();
          $("#no_calllog_info").hide();
          $("#change_trend").show();
          $("#change_trend_no_calllog_info").hide();
      } else if (d.rcode == "2" || d.cal_times == 0) {
        $("#call_log_lists").show();
        $("#show_calllog_info").hide();
        $("#no_calllog_info").show();
        $("#change_trend").hide();
       }
       $scope.$emit("NOTBUSY");
    });
  }

  /*时长统计：获取今日、全部时长统计列表*/
  $scope.getCallInfo = function(key) {
    if(key) $scope.key = key;
    if($scope.key =="today"){
          $("#demo1").addClass("selected");$("#demo2").removeClass("selected");$("#demo3").removeClass("selected");$("#demo4").removeClass("selected");
          $("#begintime").val(years+"-"+months+"-"+days);  //查看方式：今日，搜索框通话日期显示今日日期
          $("#endtime").val(years+"-"+months+"-"+days);
      }else{
         $("#demo4").addClass("selected");$("#demo1").removeClass("selected");$("#demo2").removeClass("selected");$("#demo3").removeClass("selected");
         $("#begintime").val('');
         $("#endtime").val('');
      }
      $scope.getTimeStatistics(); //获取后台时长统计timeStatistics：今日、全部
  }
  

  /*时长统计：获取本周、本月或者某时间段的时长统计列表*/
  $scope.getCallInfos = function(key) {
    if(key) $scope.key = key;
    if($scope.key =="week"){
          $("#demo2").addClass("selected");$("#demo1").removeClass("selected");$("#demo3").removeClass("selected");$("#demo4").removeClass("selected");
          $scope.search_begintime = ''; 
          $scope.search_endtime = '';
          $("#begintime").val('');                //查看方式：本周，搜索框通话日期置空
          $("#endtime").val('');
      }else if($scope.key == "month"){
          $("#demo3").addClass("selected");$("#demo1").removeClass("selected");$("#demo2").removeClass("selected");$("#demo4").removeClass("selected");
          $scope.search_begintime = '';
          $scope.search_endtime = '';
          $("#begintime").val('');                //查看方式：本月，搜索框通话日期置空
          $("#endtime").val('');
      }
      $scope.getChangeTrends();                   //获取后台时长统计(+时间段)通话列表：本周、本月、某时间段
  }

  /*时长统计：搜索事件*/
  $scope.search = function(){
    $scope.search_type = document.getElementById('c_type').value;
    $scope.search_begintime = document.getElementById('begintime').value;
    $scope.search_endtime = document.getElementById('endtime').value;
    if($scope.search_begintime && $scope.search_endtime && ($scope.search_begintime > $scope.search_endtime)){
      $scope.callRcode = 0;
      $scope.callMsg = "通话开始时间不能大于结束时间！";
      $scope.msgAttention();
    }
    else if($scope.key=='week'||$scope.key=='month'||$scope.search_begintime!=$scope.search_endtime)
       $scope.getChangeTrends();     //获取后台时长统计(+时间段)通话列表：本周、本月、某时间段
    else  
      $scope.getTimeStatistics();   //获取后台时长统计timeStatistics：今日、全部
  }

  /*页面加载默认显示今天通话时长*/
  $scope.getCallInfo('today');

  /*通话记录：获取通话记录列表*/
  $scope.ids;                   /*通话记录查看方式：打出、打入、未接、全部*/
  $scope.call_logs;             /*我的通话记录*/
  $scope.type_name;             /*通话类型*/
  $scope.search_telephones;    /*搜索：电话号码*/
  $scope.search_begintimes;   /*搜索：开始时间*/
  $scope.search_endtimes;     /*搜索：结束时间*/
  $scope.search_types;       /*搜索：通话类型*/

  $scope.getCallList = function(id){
    $scope.search_telephones = document.getElementById('telephone').value;
    $scope.search_begintimes = document.getElementById('begintimes').value;
    $scope.search_endtimes = document.getElementById('endtimes').value;
      $scope.ids = id;
      if($scope.ids =="out"){   //查看方式为打出、打入、未接、全部，不考虑通话类型，置空
          $("#demos1").addClass("selected");$("#demos2").removeClass("selected");$("#demos3").removeClass("selected");$("#demos4").removeClass("selected");
      }else if($scope.ids =="in"){
          $("#demos2").addClass("selected");$("#demos1").removeClass("selected");$("#demos3").removeClass("selected");$("#demos4").removeClass("selected");
      }else if($scope.ids == "fal"){
          $("#demos3").addClass("selected");$("#demos1").removeClass("selected");$("#demos2").removeClass("selected");$("#demos4").removeClass("selected");
      }else if($scope.ids == ""){
         $("#demos4").addClass("selected");$("#demos1").removeClass("selected");$("#demos2").removeClass("selected");$("#demos3").removeClass("selected");
      }
    
    var _url = 'call/calllogs?id='+$scope.ids+'&begintime='+$scope.search_begintimes+'&endtime='+$scope.search_endtimes+'&telephone='+$scope.search_telephones;
        _url += '&p=' + $scope.calllogpage.pageNow + '&psize=' + $scope.calllogpage.pageSize;
    $scope.$emit("BUSY");
    dataFactoryCRM.getDataByAPI(_url).then(function(d) {
      if(d.rcode==1){
        $("#show_callog_list").show();
        $("#no_callog_list").hide();
        $scope.calllogpage.pageSize = d.psize;
        $scope.calllogpage.pageAll = d.pall;
        $scope.calllogpage.pageNow = d.p;
        $scope.call_logs = d.data;
        d.data.typename = '';
        for (var st in $scope.callTypes) {
          if (parseInt($scope.callTypes[st].k) == d.data.type+1) {
            d.data.typename = $scope.callTypes[st].v;
            break;
          }
        }
      }else if(d.rcode==2){
        $("#show_callog_list").hide();
        $("#no_callog_list").show();
        $("#calllogpage_id").hide();
      }
    $scope.$emit("NOTBUSY");
    });

  }

  /*通话记录：搜索事件*/
  $scope.searchs = function(){
      //获取搜索框条件
    $scope.search_telephones = document.getElementById('telephone').value;
    $scope.search_begintimes = document.getElementById('begintimes').value;
    $scope.search_endtimes = document.getElementById('endtimes').value;
    $scope.search_types = document.getElementById('c_types').value;
    if($scope.search_types == 1){         //搜索“打出”类型，打出按钮颜色变化
      $("#demos2").addClass("selected");$("#demos1").removeClass("selected");$("#demos3").removeClass("selected");$("#demos4").removeClass("selected");
    }else if($scope.search_types == 2){   //搜索“打入”类型，打入按钮颜色变化
      $("#demos1").addClass("selected");$("#demos2").removeClass("selected");$("#demos3").removeClass("selected");$("#demos4").removeClass("selected");
    }else{                                //搜索其他类型，默认为全部
      $("#demos4").addClass("selected");$("#demos1").removeClass("selected");$("#demos2").removeClass("selected");$("#demos3").removeClass("selected");  
    }
    var _url = 'call/calllogs?telephone='+$scope.search_telephones+'&begintime='+$scope.search_begintimes+'&endtime='+$scope.search_endtimes;
        _url += '&type='+$scope.search_types+'&p=' + $scope.calllogpage.pageNow + '&psize=' + $scope.calllogpage.pageSize;
    if($scope.search_telephones || $scope.search_types){
      $scope.$emit("BUSY");
      dataFactoryCRM.getDataByAPI(_url).then(function(d) {
        if(d.rcode==1){
          $("#show_callog_list").show();
          $("#no_callog_list").hide();
          $scope.calllogpage.pageSize = d.psize;
          $scope.calllogpage.pageAll = d.pall;
          $scope.calllogpage.pageNow = d.p;
          $scope.call_logs = d.data;
          d.data.typename = '';
          for (var st in $scope.callTypes) {
            if (parseInt($scope.callTypes[st].k) == d.data.type+1) {
              d.data.typename = $scope.callTypes[st].v;
              break;
            }
          }
        }else if(d.rcode==2){
          $("#show_callog_list").hide();
          $("#no_callog_list").show();
        }
        $scope.$emit("NOTBUSY");
      });
    }
    if($scope.search_begintimes && $scope.search_endtimes && $scope.search_begintimes <= $scope.search_endtimes){
        dataFactoryCRM.getDataByAPI(_url).then(function(d) {
        if(d.rcode==1){
          $("#show_callog_list").show();
          $("#no_callog_list").hide();
          $scope.calllogpage.pageSize = d.psize;
          $scope.calllogpage.pageAll = d.pall;
          $scope.calllogpage.pageNow = d.p;
          $scope.call_logs = d.data;
          d.data.typename = '';
          for (var st in $scope.callTypes) {
            if (parseInt($scope.callTypes[st].k) == d.data.type+1) {
              d.data.typename = $scope.callTypes[st].v;
              break;
            }
          }
        }else if(d.rcode==2){
          $("#show_callog_list").hide();
          $("#no_callog_list").show();
        }
      });
    } 
    else if($scope.search_begintimes > $scope.search_endtimes){
      $scope.callRcode = 0;
      $scope.callMsg = "通话开始时间不能大于结束时间！";
      $scope.msgAttention();
    }
  }
  /*加载页面默认显示打出通话记录*/
  $scope.getCallList('');
  
  });

});
